package com.example.demo.poi;

import com.alibaba.excel.EasyExcel;
import com.example.demo.vo.EasyExcelVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
public class EasyExcelController {

    /**
     *
     */
    @GetMapping("/test")
    public void test(HttpServletResponse response) throws IOException {
        List<EasyExcelVo> data = new ArrayList<>();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("文本素材导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), EasyExcelVo.class)
                .inMemory(true)
                .registerWriteHandler(new CustomCellWriteHandler())
                .sheet("模板")
                .doWrite(data);
    }

    @GetMapping("/read")
    public void read(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), EasyExcelVo.class, new EasyExcelDataListener()).sheet().headRowNumber(2).doRead();
    }


}
